/**
 * 
 */
package com.achievo.know.web.auth;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.AuthApplication;
import com.achievo.know.application.SyncLogApplication;
import com.achievo.know.application.UserApplication;
import com.achievo.know.domain.Role;
import com.achievo.know.domain.SyncLog;
import com.achievo.know.domain.User;
import com.achievo.know.pojo.SearchParam;
import com.achievo.know.web.util.PageActionSupport;
import com.dayatang.querychannel.support.Page;
import com.redhat.redwork.coc.RedworkAction;

/**
 * @author hp
 * 
 */
public class AuthAction extends PageActionSupport implements RedworkAction {
	
	Logger logger=LoggerFactory.getLogger(AuthAction.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -2518774079299231924L;

	@Autowired
	private AuthApplication authApplication;
	@Autowired
	private UserApplication userApplication;

	private Page<Role> rolePage;
	private Page<User> userPage;
	private String curTab="ROLE";//当前TAB
	private String ids;//选择删除的IDS
	private String jsonResult;//JSON操作结果
	private String code;
	private Role role;
	private String id;
	private String password;
	private String password1;
	
	
	

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String home() {
		if("ROLE".equals(curTab))
			rolePage=authApplication.queryRolePage(currentPage, pageSize);
		else{
			userPage=authApplication.queryUserPage(currentPage, pageSize,params);
			roles=authApplication.getRoles();
		}
		return "home";
	}
	
	public String addRole(){
		try{
			authApplication.saveRole(role);
			jsonResult="保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String editRole(){
		try{
			Role r=authApplication.getRole(role.getId());
			r.setName(role.getName());
			authApplication.saveRole(r);
			jsonResult="保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String delRole(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idArr=ids.split(",");
				authApplication.batchDeleteRole(idArr);
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}

	public String delUser(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idArr=ids.split(",");
				authApplication.batchDeleteUser(idArr);
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String addUser(){
		try{
			authApplication.saveUser(user);
			jsonResult="保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	public String changePwd(){
		try{
			User olduser=authApplication.getUser(user.getId());
			if(!user.getPassword().equals(olduser.getPassword())){
				jsonResult="原密码出错!";
				return JSON;
			}
			olduser.setPassword(password);
			authApplication.saveUser(olduser);
			jsonResult="保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	public String showAddUser(){
		if(StringUtils.isNotEmpty(id)){
			user=authApplication.getUser(Long.parseLong(id));
		}
		return "add";
	}
	public String showChangePwd(){
		if(StringUtils.isNotEmpty(id)){
			user=userApplication.get(id);
		}
		return "changepwd";
	}
	public String editUser(){
		try{
			User u=authApplication.getUser(user.getId());
			u.setUsername(user.getUsername());
			u.setPassword(user.getPassword());
			u.setRealName(user.getRealName());
			authApplication.saveUser(u);
			jsonResult="保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String showFuncs(){
		data=authApplication.getFuncJson(role.getId());
		return "show-funcs";
	}
	
	public String showRoles(){
		data=authApplication.getRolesJson(user.getId());
		return "show-roles";
	}
	
	public String showCates(){
		data=authApplication.getCatesJson(user.getId());
		return "show-cates";
	}
	
	public String showRoleCates(){
		data=authApplication.getRoleCatesJson(role.getId());
		return "show-cates";
	}
	
	public String setFuncs(){
		try{
				String[] idArr=ids.split(",");
				authApplication.batchSetFunction(role.getId()+"", idArr);
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String setRoles(){
		try{
				String[] idArr=ids.split(",");
				authApplication.batchSetRole(user.getId()+"", idArr);
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String setCates(){
		try{
				String[] idArr=ids.split(",");
				authApplication.setCategorys(user.getId()+"", idArr);
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String setRoleCates(){
		try{
				String[] idArr=ids.split(",");
				authApplication.setRoleCategorys(role.getId()+"", idArr);
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String batchSetFunc(){
		try{
				String[] idArr=ids.split(",");
				String[] fidArr=fids.split(",");
				for(String rid:idArr){
					authApplication.batchSetFunction(rid+"", fidArr);
				}
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String batchSetRole(){
		try{
				String[] idArr=ids.split(",");
				String[] ridArr=rids.split(",");
				for(String uid:idArr){
					authApplication.batchSetRole(uid+"", ridArr);
				}
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String batchSetCate(){
		try{
				String[] idArr=ids.split(",");
				String[] cidArr=cids.split(",");
				for(String uid:idArr){
					authApplication.setCategorys(uid+"", cidArr);
				}
				jsonResult="设置成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String syncLog(){
		logPage=syncLogApplication.queryPage(currentPage, pageSize);
		return "sync_log";
	}
	
	private User user;
	
	private String data;
	
	private String fids;
	private String rids;
	private String cids;
	private List<Role> roles;
	private SearchParam params=new SearchParam();
	private Page<SyncLog> logPage;
	@Autowired
	private SyncLogApplication syncLogApplication;
	
	
	public void setSyncLogApplication(SyncLogApplication syncLogApplication) {
		this.syncLogApplication = syncLogApplication;
	}

	public Page<SyncLog> getLogPage() {
		return logPage;
	}

	public void setLogPage(Page<SyncLog> logPage) {
		this.logPage = logPage;
	}

	public SearchParam getParams() {
		return params;
	}

	public void setParams(SearchParam params) {
		this.params = params;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public String getCids() {
		return cids;
	}

	public void setCids(String cids) {
		this.cids = cids;
	}

	public String getRids() {
		return rids;
	}

	public void setRids(String rids) {
		this.rids = rids;
	}

	public String getFids() {
		return fids;
	}

	public void setFids(String fids) {
		this.fids = fids;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Page<Role> getRolePage() {
		return rolePage;
	}

	public void setRolePage(Page<Role> rolePage) {
		this.rolePage = rolePage;
	}

	public void setAuthApplication(AuthApplication authApplication) {
		this.authApplication = authApplication;
	}

	public String getCurTab() {
		return curTab;
	}

	public void setCurTab(String curTab) {
		this.curTab = curTab;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Page<User> getUserPage() {
		return userPage;
	}

	public void setUserPage(Page<User> userPage) {
		this.userPage = userPage;
	}
	
	@Override
	public String getParam() {
		StringBuilder b=new StringBuilder();
		b.append("curTab="+curTab);
		if(StringUtils.isNotEmpty(params.getRoleName())){
			b.append("&params.roleName="+params.getRoleName());
		}
		if(StringUtils.isNotEmpty(params.getDept())){
			b.append("&params.dept="+params.getDept());
		}
		return b.toString();
	}

	public void setUserApplication(UserApplication userApplication) {
		this.userApplication = userApplication;
	}

	
}
